package com.zimmor.mq.rocket.base.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * @Author:zimmor
 * @Date: 2021/11/18 14:34
 * @Description 发送同步消息
 * @version: 1.0.0
 */

public class SyncProducer {
    public static void main(String[] args) throws Exception {
       //发送消息的步骤
       //1.创建一个producer 设置producer的生产者组名groupID，
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.设置namesrv地址
        producer.setNamesrvAddr("192.168.204.136:9876;192.168.204.137:9876");
       //3.启动producer
        producer.start();

        for (int i = 0; i < 10; i++) {
            //4.新建消息对象。并设置topic和tag和消息体
            Message message = new Message("base", "tag1", ("hello RocketMQ" + i).getBytes());
            //5.发送消息
            SendResult result = producer.send(message);
            System.out.println("result = " + result);
        }
       //6.关闭producer
        producer.shutdown();
    }
}
